package com.shuaibiao.controller.base;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shuaibiao.entity.base.BaseRole;
import com.shuaibiao.entity.base.Constants;
import com.shuaibiao.entity.base.JsonResult;
import com.shuaibiao.service.base.BaseRoleService;

/**
 * 基本基于RESTFUL
 * @author Y
 *
 */
@RestController
@RequestMapping("/baseRole")
public class BaseRoleController {
	
	@Autowired
	private BaseRoleService service;
	
	/**
	 * 新增
	 * @param record
	 * @return
	 */
	@RequestMapping(value="/role",method = RequestMethod.POST)
	public JsonResult insert(BaseRole record){
		record.setCreatetime(new Date());
		record.setIsuse(Constants.ISUSE_Y);
		service.insert(record);
		return new JsonResult();
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deleteRole",method = RequestMethod.PUT)
	public JsonResult isuseThisRecord(String idStr){
		String[] idArr = idStr.split(",");
		for (String id : idArr) {
			service.isuseThisRecord(Integer.parseInt(id));			
		}
		return new JsonResult();
	}
	
	/**
	 * 更新
	 * @param record
	 * @return
	 */
	@RequestMapping(value="/role",method = RequestMethod.PUT)
	public JsonResult updateByPrimaryKeySelective(BaseRole record){
		record.setUpdatetime(new Date());
		service.updateByPrimaryKeySelective(record);
		return new JsonResult();
	}
	
	/**
	 * 查询
	 * @param record
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping(value="/role",method = RequestMethod.GET)
	public JsonResult pageByExample(BaseRole record,int page,int limit){
		record.setIsuse("Y");
		PageHelper.startPage(page, limit);
		List<BaseRole> list = service.selectAll(record);
		PageInfo<BaseRole> pageInfo = new PageInfo<BaseRole>(list);
		JsonResult res = new JsonResult();
		res.setCode(Constants.OK);
		res.setData(list);
		res.setCount(pageInfo.getTotal());
		return res;
	}

}
